﻿
// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});
var mustEqual = function (val, other) {
	return val == other();
};

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.Fullname = ko.observable(data.UserInfo.Fullname).extend({ maxLength: 50 });
    this.FirstName = ko.observable(data.UserInfo.FirstName).extend({ maxLength: 50 });
    this.LastName = ko.observable(data.UserInfo.LastName).extend({ maxLength: 50 });
    this.Email = ko.observable(data.UserInfo.Email);
    this.Password = ko.observable(data.UserInfo.Password);
    this.ConfirmPassword = ko.observable(data.UserInfo.Password);

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.ConfirmPassword = ko.observable().extend({
	validation: { validator: mustEqual, message: 'Mật khẩu không chính xác.', params: viewModel.Password }
}),
viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));